import { _decorator, Component, Node, find, Vec3, Vec2, RigidBody2D, Prefab, instantiate, UITransform, math, Collider2D, sp, AudioSource, AudioClip } from 'cc';
import { Enemy } from './Enemy/Enemy';
const { ccclass, property } = _decorator;

@ccclass('Homie')
export class Homie extends Component {
    @property(Prefab)
    BulletPrefab: Prefab;
    private player: Node = null;
    private bulletLayer = null;

    /**动画组件 */
    @property({ type: sp.Skeleton })
    public skeleton: sp.Skeleton;

    @property(AudioSource)
    public audioSource: AudioSource = null;


    @property(AudioClip)
    public attackClip: AudioClip = null;

    private attackTimer = 0;
    private attackCooldown = 2;

    @property
    private moveSpeed: number = 5;

    @property
    private followDistance: number = 200;
    private attackRange: number = 1000;

    private rb: RigidBody2D = null;

    start() {
        this.rb = this.getComponent(RigidBody2D);

        if (!this.player) {
            this.player = find('Canvas/Player');
        }
        this.bulletLayer = find('Canvas/BulletLayer');
    }

    update(deltaTime: number) {
        if (!this.player) return;
        this.moveToPlayer(deltaTime);
        // 更新技能冷却计时器
        this.attackTimer += deltaTime;

        // 如果冷却时间结束，释放技能并重置计时器
        if (this.attackTimer >= this.attackCooldown) {
            this.skeleton.setAnimation(0, 'gongji', false);
            this.spawnRandomSkillEffect();
            this.attackTimer = 0;
        }
    }

    private moveToPlayer(dt: number) {
        const direction = new Vec3();
        Vec3.subtract(direction, this.player.worldPosition, this.node.worldPosition);
        const distance = direction.length();
        if (distance > this.followDistance) {
            direction.normalize();
            const velocity = new Vec2(direction.x, direction.y);
            velocity.multiplyScalar(this.moveSpeed);
            if (this.rb) {
                this.rb.linearVelocity = velocity;
            } else {
                const newPos = new Vec3();
                Vec3.add(newPos, this.node.position, direction.multiplyScalar(this.moveSpeed * dt));
                this.node.setPosition(newPos);
            }
        } else if (this.rb) {
            this.rb.linearVelocity = Vec2.ZERO;
        }

    }

    private spawnRandomSkillEffect() {
        const enemyLayer = find('Canvas/EnemyLayer');
        const enemies = enemyLayer.children;
        for (const enemy of enemies) {

            const direction = new Vec3();
            // 使用世界坐标计算方向
            Vec3.subtract(direction, enemy.worldPosition, this.node.worldPosition);
            const distance = direction.length();

            if (distance <= this.attackRange) {
                const skillEffect = instantiate(this.BulletPrefab);
                skillEffect.setWorldPosition(enemy.position);
                this.bulletLayer.addChild(skillEffect);
                this.audioSource.playOneShot(this.attackClip, 0.2)
                enemy.getComponent(Enemy).TakeDamage(10);
                setTimeout(() => {
                    skillEffect.destroy();
                }, 1000);
            }
        }
    }







}
